<?php
/**
 * @name Bootstrap
 * @author root
 * @desc 所有在Bootstrap类中, 以_init开头的方法, 都会被Yaf调用,
 * @see http://www.php.net/manual/en/class.yaf-bootstrap-abstract.php
 * 这些方法, 都接受一个参数:Yaf_Dispatcher $dispatcher
 * 调用的次序, 和申明的次序相同
 */
use Yaf\Bootstrap_Abstract;
use Yaf\Application;
use Yaf\Registry;
use Yaf\Dispatcher;
use Yaf\Loader;

use Illuminate\Database\Capsule\Manager as Capsule;

class Bootstrap extends Bootstrap_Abstract{

    /**
     * composer自动加载
     */
    public function _initLoad(){

    }

    /**
     * 保存配置
     */
    public function _initConfig() {
		$arrConfig = Application::app()->getConfig()->toArray();
		Registry::set('config', $arrConfig);
	}

    /**
     * 注册插件
     * @param \Yaf\Dispatcher $dispatcher
     */
	public function _initPlugin(Dispatcher $dispatcher) {
        $dispatcher->registerPlugin(new ModuleBootstrapPlugin());
		//$objSamplePlugin = new SamplePlugin();
		//$dispatcher->registerPlugin($objSamplePlugin);
	}

    /**
     * 定制路由
     * @param \Yaf\Dispatcher $dispatcher
     */
	public function _initRoute(Dispatcher $dispatcher) {
		//在这里注册自己的路由协议,默认使用简单路由
        $config = require(APP_PATH . '/conf/routes.php');
        $router = $dispatcher->getRouter();
        $router->addConfig($config);
	}


    /**
     * Eloquent orm 初始化
     */
    public function _initDb()
    {
        $db_config = Registry::get('config');
        $db_config = $db_config['database'];
        $capsule = new Capsule;
        // 创建链接
        $capsule->addConnection($db_config);
        // 设置全局静态可访问
        $capsule->setAsGlobal();
        $capsule->bootEloquent();

        class_alias('\Illuminate\Database\Capsule\Manager', 'DB');

        $env = Application::app()->environ();

        if ($env != 'product') {
            $capsule->getConnection()->enableQueryLog();
        }
    }

    /**
     * @param Dispatcher $dispatcher
     */
    public function _initView(Dispatcher $dispatcher)
    {
        // 不自动渲染视图
        $dispatcher->autoRender(false);
    }
}
